﻿package mortal.game.scene3D.display3d.text3d.dynamicText3d
{
    import __AS3__.vec.*;
    import baseEngine.core.*;
    import baseEngine.system.*;
    import flash.events.*;
    import flash.geom.*;
    import frEngine.*;
    import frEngine.event.*;
    import mortal.game.scene3D.display3d.text3d.*;
    import mortal.game.scene3D.layer3D.utils.*;

    public class Text3D extends Pivot3D
    {
        public var targetFollow:Pivot3D;
        public var place:Text3DPlace;
        private var _targetVector:Vector.<Number>;
        private var _ix:int;
        private var _iy:int;
        private var _iz:int;
        private var _iw:int;
        public var textValue:String;
        public var bigImg:Text3DBigImg;
        private var _offsetY:uint = 0;
        public var textWidth:int = 0;
        public var textHeight:int = 0;
        private var _updataPos:Boolean = false;
        private var posy:Number = 0;
        public var needMoving:Boolean;
        private var _preframe:int = -1;
        private var startFrame:uint;
        private var _x:Number = 0;
        private var _y:Number = 0;
        private var _z:Number = 0;
        private static var _temp0:Vector3D = new Vector3D();

        public function Text3D()
        {
            super("");
            this.enabledPool = false;
            FrEventDispatcher.instance.proxyAddEventListener(this, Engine3dEventName.VISIBLE_CHANGE_EVENT, this.visibleChangeHander);
            return;
        }// end function

        override public function dispose(param1:Boolean = true) : void
        {
            super.dispose(param1);
            FrEventDispatcher.instance.proxyRemoveEventListener(this, Engine3dEventName.VISIBLE_CHANGE_EVENT, this.visibleChangeHander);
            return;
        }// end function

        private function visibleChangeHander(event:Event) : void
        {
            if (this.visible)
            {
                this.show();
            }
            else
            {
                this.hide();
            }
            return;
        }// end function

        public function clear() : void
        {
            this.bigImg = null;
            this._targetVector = null;
            this.needMoving = false;
            FrEventDispatcher.instance.proxyRemoveEventListener(this, Engine3dEventName.VISIBLE_CHANGE_EVENT, this.visibleChangeHander);
            this.targetFollow = null;
            return;
        }// end function

        public function reInit(param1:Text3DBigImg, param2:Text3DPlace, param3:Boolean, param4:Pivot3D) : void
        {
            this.bigImg = param1;
            if (param2)
            {
                this.place = param2;
                this._targetVector = this.place.targetVector;
                this._ix = this.place.placeId * 4;
                this._iy = this._ix + 1;
                this._iz = this._ix + 2;
                this._iw = this._ix + 3;
                this._targetVector[this._ix] = 0;
                this._targetVector[this._iy] = 0;
                this._targetVector[this._iz] = 0;
                this._targetVector[this._iw] = 0;
            }
            this.needMoving = param3;
            if (this.needMoving)
            {
                this.startFrame = TimeControler.stageFrame;
            }
            this.targetFollow = param4;
            if (this.parent != Global3D.scene)
            {
                Global3D.scene.addChild(this);
            }
            if (this._targetVector.length > 400)
            {
                throw new Error("超出范围");
            }
            return;
        }// end function

        private function updatePos(event:Event = null) : void
        {
            this._updataPos = true;
            this.update();
            return;
        }// end function

        public function setImgPos(param1:uint, param2:uint, param3:uint, param4:String) : void
        {
            this.textHeight = param3;
            this.textWidth = param2;
            this._offsetY = param1;
            this.textValue = param4;
            this._targetVector[this._iz] = this._offsetY + 0.99;
            return;
        }// end function

        public function show() : void
        {
            if (this.targetFollow)
            {
                this._targetVector[this._iw] = this.textHeight;
                this._updataPos = true;
                FrEventDispatcher.instance.proxyAddEventListener(this.targetFollow, Engine3dEventName.UPDATE_TRANSFORM_EVENT, this.updatePos);
            }
            isHide = false;
            return;
        }// end function

        public function hide() : void
        {
            this._targetVector[this._iw] = -1;
            isHide = true;
            this._updataPos = true;
            return;
        }// end function

        override public function set x(param1:Number) : void
        {
            this._x = param1;
            this._updataPos = true;
            return;
        }// end function

        override public function set y(param1:Number) : void
        {
            this._y = param1;
            this._updataPos = true;
            return;
        }// end function

        override public function set z(param1:Number) : void
        {
            this._z = param1;
            this._updataPos = true;
            return;
        }// end function

        override public function get x() : Number
        {
            return this._x;
        }// end function

        override public function get y() : Number
        {
            return this._y;
        }// end function

        override public function get z() : Number
        {
            return this._z;
        }// end function

        override public function update(param1:int = 0) : void
        {
            var _loc_2:Vector3D = null;
            var _loc_3:Point = null;
            var _loc_4:int = 0;
            if (this._updataPos)
            {
                _loc_2 = this.targetFollow.getPosition(false, _temp0);
                _loc_2.x = _loc_2.x + this._x;
                _loc_2.y = _loc_2.y + this._y;
                _loc_2.z = _loc_2.z + this._z;
                _loc_3 = Scene3DUtil.change3Dto2D(_loc_2);
                this._targetVector[this._ix] = _loc_3.x;
                var _loc_5:* = _loc_3.y;
                this.posy = _loc_3.y;
                this._targetVector[this._iy] = _loc_5;
                this._updataPos = false;
            }
            if (this.needMoving)
            {
                _loc_4 = TimeControler.stageFrame - this.startFrame;
                if (this._preframe == _loc_4)
                {
                    return;
                }
                this._preframe = _loc_4;
                this._targetVector[this._iy] = this.posy - _loc_4 * 1;
                if (_loc_4 <= 45)
                {
                    this._targetVector[this._iz] = this._offsetY + 0.99;
                }
                else if (_loc_4 < 65)
                {
                    this._targetVector[this._iz] = this._offsetY + (65 - _loc_4) / 20;
                }
                else
                {
                    Text3DFactory.instance.disposeText3D(this);
                }
            }
            return;
        }// end function

        override public function updateChildrenTransform(param1:int = 0) : void
        {
            return;
        }// end function

        override public function updateTransforms(param1:Boolean, param2:Boolean, param3:int = 0) : void
        {
            return;
        }// end function

    }
}
